﻿$(document).ready(function () {

    var reste = $("#cntContentPage_txtReste").val();
    var myFkKingdom = $("#cntContentPage_myFkKingdom").val();

    function SetWork(FkKingdom, Disciple, Lumberjack, Miner, Farmer, Worker) {
        var jsonText = JSON.stringify({ PkKingdom: FkKingdom, Disciple: Disciple, Lumberjack: Lumberjack, Miner: Miner, Farmer: Farmer, Worker: Worker });

        $.ajax({
            type: "POST",
            url: "ajax/ajax.aspx/SetWork",
            data: jsonText,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                if (msg.d == "True") {
                    GetWork(FkKingdom);
                }
                else {
                    alert("Error SQL");
                }
            }
        });
    }

    function GetWork(FkKingdom) {
        var jsonText = JSON.stringify({ FkKingdom: FkKingdom });

        $.ajax({
            type: "POST",
            url: "ajax/ajax.aspx/GetWork",
            data: jsonText,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                $("#cntContentPage_Label1").html(msg.d);
            }
        });
    }

    $("#population > span").each(function () {
        // pour chaque span de la div, fait un slider
        var value = parseInt($(this).text(), 10);

        var max;
        var parentSpan;
        $(this).empty().slider({
            value: value,
            min: 0,
            max: 100,
            range: "min",
            animate: true,
            orientation: "vertical",
            monMax: 0,
            start: function (event, ui) {
                monMax = parseInt(ui.value) + parseInt(reste);
                reste = parseInt(monMax) - parseInt(ui.value);
            },
            slide: function (event, ui) {
                $("#cntContentPage_txtReste").val(monMax - ui.value);
                if (ui.value >= monMax) {
                    ui.value = monMax;
                    $(this).slider("option", "value", monMax);
                    $("#cntContentPage_txtReste").val(0);
                }
                parentSpan = "txt" + $(this).closest("span").attr("id").substring(15);
                $("#cntContentPage_" + parentSpan).val(ui.value);
            },
            change: function (event, ui) {
                var Disciple = $("#cntContentPage_txtDisciple").val();
                var Lumberjack = $("#cntContentPage_txtLumberjack").val();
                var Miner = $("#cntContentPage_txtMiner").val();
                var Farmer = $("#cntContentPage_txtFarmer").val();
                var Worker = $("#cntContentPage_txtWorker").val();

                SetWork(myFkKingdom, Disciple, Lumberjack, Miner, Farmer, Worker);
            },
            stop: function (event, ui) {
                reste = monMax - ui.value;
                $("#cntContentPage_txtReste").val(reste);
                if (reste < 0) {
                    reste = 0;
                    $("#cntContentPage_txtReste").val(0);
                    $(this).slider("option", "value", monMax);
                    $("#cntContentPage_" + parentSpan).val(monMax);
                }

            }

        });

    });
});